import sys

from PySide6.QtCore import Qt, QSize, QRect
from PySide6.QtGui import QPainter, QColor
from PySide6.QtWidgets import QHBoxLayout, QWidget
from qfluentwidgets import qconfig, isDarkTheme, MSFluentTitleBar
from qfluentwidgets.common.animation import BackgroundAnimationWidget
from qfluentwidgets.components.widgets.frameless_window import FramelessWindow

from ..widgets import SlidingStackedWidget


class FluentWindowBase(BackgroundAnimationWidget, FramelessWindow):
    """ Fluent window base class """

    def __init__(self, parent=None):
        self._isMicaEnabled = False
        self._lightBackgroundColor = QColor(240, 244, 249)
        self._darkBackgroundColor = QColor(32, 32, 32)
        super().__init__(parent=parent)
        self.setTitleBar(MSFluentTitleBar(self))
        self.__hBoxLayout = QHBoxLayout(self)
        self.stackedWidget = SlidingStackedWidget(self)

        self.__hBoxLayout.setContentsMargins(0, self.titleBar.height(), 0, 0)
        self.__hBoxLayout.addWidget(self.stackedWidget)

        # show system title bar buttons on macOS
        if sys.platform == "darwin":
            self.setSystemTitleBarButtonVisible(True)

        qconfig.themeChangedFinished.connect(self._onThemeChangedFinished)

    def _normalBackgroundColor(self):
        if not self.isMicaEffectEnabled():
            return self._darkBackgroundColor if isDarkTheme() else self._lightBackgroundColor

        return QColor(0, 0, 0, 0)

    def _onThemeChangedFinished(self):
        if self.isMicaEffectEnabled():
            self.windowEffect.setMicaEffect(self.winId(), isDarkTheme())

    def paintEvent(self, e):
        super().paintEvent(e)
        painter = QPainter(self)
        painter.setPen(Qt.PenStyle.NoPen)
        painter.setBrush(self.backgroundColor)
        painter.drawRect(self.rect())

    def setMicaEffectEnabled(self, isEnabled: bool):
        """ set whether the mica effect is enabled, only available on Win11 """
        if sys.platform != 'win32' or sys.getwindowsversion().build < 22000:
            return

        self._isMicaEnabled = isEnabled

        if isEnabled:
            self.windowEffect.setMicaEffect(self.winId(), isDarkTheme())
        else:
            self.windowEffect.removeBackgroundEffect(self.winId())

        self.setBackgroundColor(self._normalBackgroundColor())

    def isMicaEffectEnabled(self):
        return self._isMicaEnabled

    def systemTitleBarRect(self, size: QSize) -> QRect:
        """ Returns the system title bar rect, only works for macOS

        Parameters
        ----------
        size: QSize
            original system title bar rect
        """
        return QRect(size.width() - 75, 0 if self.isFullScreen() else 9, 75, size.height())

    def setTitleBar(self, titleBar):
        super().setTitleBar(titleBar)

        # hide title bar buttons on macOS
        if sys.platform == "darwin" and self.isSystemButtonVisible() and isinstance(titleBar, TitleBarBase):
            titleBar.minBtn.hide()
            titleBar.maxBtn.hide()
            titleBar.closeBtn.hide()

    def addWidget(self, widget: QWidget):
        self.stackedWidget.addWidget(widget)

    def switchTo(self, interface: QWidget):
        self.stackedWidget.setCurrentWidget(interface)

    # def setAcrylicEnabled(self, isEnabled: bool):
    #     if not isEnabled:
    #         return
    #     if not isDarkTheme():
    #         self.windowEffect.setAcrylicEffect(self.winId(), "f0f4f930", isEnabled, 0)
    #     else:
    #         self.windowEffect.setAcrylicEffect(self.winId(), "20202030", isEnabled, 0)
